/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.stark.commons.spring.core.support.ioc;

import java.util.Map;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.lang.Nullable;

import com.stark.commons.spring.core.context.ApplicationContextUtil;

/**
 * 定义 Bean 接口。
 * <p>实现该接口，可通过 {@link ApplicationContextUtil#registerBean(BeanDefinition)} 动态注入到 IOC 容器。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.0
 * @see com.stark.commons.spring.core.context.ApplicationContextUtil#registerBean(BeanDefinition)
 * @see org.springframework.beans.factory.config.BeanDefinition
 */
public interface BeanDefinition {

	/**
	 * Scope identifier for the standard singleton scope: "singleton".
	 * <p>Note that extended bean factories might support further scopes.
	 * @see #setScope
	 */
	String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

	/**
	 * Scope identifier for the standard prototype scope: "prototype".
	 * <p>Note that extended bean factories might support further scopes.
	 * @see #setScope
	 */
	String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;


	/**
	 * Role hint indicating that a {@code BeanDefinition} is a major part
	 * of the application. Typically corresponds to a user-defined bean.
	 */
	int ROLE_APPLICATION = 0;

	/**
	 * Role hint indicating that a {@code BeanDefinition} is a supporting
	 * part of some larger configuration, typically an outer
	 * {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
	 * {@code SUPPORT} beans are considered important enough to be aware
	 * of when looking more closely at a particular
	 * {@link org.springframework.beans.factory.parsing.ComponentDefinition},
	 * but not when looking at the overall configuration of an application.
	 */
	int ROLE_SUPPORT = 1;

	/**
	 * Role hint indicating that a {@code BeanDefinition} is providing an
	 * entirely background role and has no relevance to the end-user. This hint is
	 * used when registering beans that are completely part of the internal workings
	 * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
	 */
	int ROLE_INFRASTRUCTURE = 2;
	
	/**
	 * Constant that indicates no autowiring at all.
	 * @see #setAutowireMode
	 */
	int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;

	/**
	 * Constant that indicates autowiring bean properties by name.
	 * @see #setAutowireMode
	 */
	int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;

	/**
	 * Constant that indicates autowiring bean properties by type.
	 * @see #setAutowireMode
	 */
	int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;

	/**
	 * Constant that indicates autowiring a constructor.
	 * @see #setAutowireMode
	 */
	int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
	
	/**
	 * Constant that indicates no dependency check at all.
	 * @see #setDependencyCheck
	 */
	public static final int DEPENDENCY_CHECK_NONE = 0;

	/**
	 * Constant that indicates dependency checking for object references.
	 * @see #setDependencyCheck
	 */
	public static final int DEPENDENCY_CHECK_OBJECTS = 1;

	/**
	 * Constant that indicates dependency checking for "simple" properties.
	 * @see #setDependencyCheck
	 * @see org.springframework.beans.BeanUtils#isSimpleProperty
	 */
	public static final int DEPENDENCY_CHECK_SIMPLE = 2;

	/**
	 * Constant that indicates dependency checking for all properties
	 * (object references as well as "simple" properties).
	 * @see #setDependencyCheck
	 */
	public static final int DEPENDENCY_CHECK_ALL = 3;
	
	/**
	 * 获取 bean 所属类名。
	 * @return bean 所属类名。
	 */
	String getBeanClassName();
	
	/**
	 * 获取 bean 注册名称。
	 * @return bean 注册名称。
	 */
	String getBeanName();
	
	/**
	 * 获取属性集合。
	 * @return 属性集合。
	 */
	@Nullable
	Map<String, Object> getProperties();

	/**
	 * 获取父类名。
	 */
	default String getParentName() {
		return null;
	}

	/**
	 * 获取作用域，单例 {@value #SCOPE_SINGLETON} 或多例 {@value #SCOPE_PROTOTYPE} 。
	 */
	default String getScope() {
		return SCOPE_SINGLETON;
	}

	/**
	 * 是否懒加载，仅对单例 bean 有效。
	 */
	default boolean isLazyInit() {
		return false;
	}
	
	/**
	 * 获取依赖检查方式。
	 */
	default int getDependencyCheck() {
		return DEPENDENCY_CHECK_NONE;
	}

	/**
	 * 获取依赖的 bean 名称。
	 */
	default String[] getDependsOn() {
		return null;
	}
	
	/**
	 * 是否支持自动注入到其它 bean 。
	 */
	default boolean isAutowireCandidate() {
		return true;
	}
	
	/**
	 * 是否支持自动注入到其它 bean 。
	 * @see 
	 */
	default int getAutowireMode() {
		return AUTOWIRE_NO;
	}

	/**
	 * 是否主 bean 。
	 */
	default boolean isPrimary() {
		return false;
	}

	/**
	 * 获取工厂 bean 名称。
	 */
	default String getFactoryBeanName() {
		return null;
	}

	/**
	 * 获取工厂方法名。
	 */
	default String getFactoryMethodName() {
		return null;
	}

	/**
	 * 获取初始化方法名。
	 */
	default String getInitMethodName() {
		return null;
	}

	/**
	 * 获取析构方法名。
	 */
	default String getDestroyMethodName() {
		return null;
	}

	/**
	 * 获取 bean 角色。
	 * @see #ROLE_APPLICATION
	 * @see #ROLE_SUPPORT
	 * @see #ROLE_INFRASTRUCTURE
	 */
	default int getRole() {
		return ROLE_APPLICATION;
	}

	/**
	 * 是否抽象 bean , 即不实例化。
	 */
	default boolean isAbstract() {
		return false;
	}

}
